package com.livery.data.creators;

import javax.swing.JFrame;

import com.livery.app.main.HibernateUtil;
import com.livery.data.Car;
import com.livery.data.Client;
import com.livery.data.Transaction;
import com.livery.data.Worker;
import com.livery.login.SessionContext;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDialog;

import java.awt.GridBagLayout;
import javax.swing.BoxLayout;
import javax.swing.JSpinner;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JSpinnerDateEditor;

import javax.swing.LayoutStyle.ComponentPlacement;

import org.hibernate.Session;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TransactionCreator extends JDialog implements WindowListener,ActionListener,PropertyChangeListener,ChangeListener, Creator<Transaction> {
@Deprecated	
public TransactionCreator() {
		this.setModal(true);
		this.setModalityType(ModalityType.APPLICATION_MODAL);
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_10 = new JPanel();
		panel.add(panel_10);
		panel_10.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_3 = new JPanel();
		panel_10.add(panel_3);
		panel_3.setLayout(new GridLayout(0, 2, 0, 0));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Samoch\u00F3d", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, null));
		panel_3.add(panel_1);
		panel_1.setLayout(new GridLayout(0, 1, 0, 0));
		
		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Model", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(190, 30, 30)));
		panel_1.add(panel_4);
		panel_4.setLayout(new BorderLayout(0, 0));
		
		lblModel = new JLabel("Model");
		panel_4.add(lblModel);
		
		JPanel panel_5 = new JPanel();
		panel_5.setBorder(new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Marka", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(190, 30, 30)));
		panel_1.add(panel_5);
		panel_5.setLayout(new BorderLayout(0, 0));
		
		lblMaker = new JLabel("Marka");
		panel_5.add(lblMaker);
		
		JPanel panel_6 = new JPanel();
		panel_6.setBorder(new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Numer rejestracyjny", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(190, 30, 30)));
		panel_1.add(panel_6);
		panel_6.setLayout(new BorderLayout(0, 0));
		
		JLabel lblRegister = new JLabel("register");
		panel_6.add(lblRegister);
		
		JPanel panel_16 = new JPanel();
		panel_16.setBorder(new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Kaucja", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(190, 30, 30)));
		panel_1.add(panel_16);
		panel_16.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		
		lblDeposit = new JLabel("1000zl");
		panel_16.add(lblDeposit);
		
		JPanel panel_13 = new JPanel();
		panel_13.setBorder(new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Cena za dzie\u0144", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(190, 30, 30)));
		panel_1.add(panel_13);
		panel_13.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		
		lblPrice = new JLabel("100zl");
		panel_13.add(lblPrice);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Klient", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		panel_3.add(panel_2);
		panel_2.setLayout(new GridLayout(3, 0, 0, 0));
		
		JPanel panel_7 = new JPanel();
		panel_7.setBorder(new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Imi\u0119", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(190, 30, 30)));
		panel_2.add(panel_7);
		panel_7.setLayout(new BorderLayout(0, 0));
		
		lblName = new JLabel("Name");
		panel_7.add(lblName, BorderLayout.NORTH);
		
		JPanel panel_9 = new JPanel();
		panel_9.setBorder(new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Nazwisko", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(190, 30, 30)));
		panel_2.add(panel_9);
		panel_9.setLayout(new BorderLayout(0, 0));
		
		lblSurname = new JLabel("Surname");
		panel_9.add(lblSurname, BorderLayout.NORTH);
		
		JPanel panel_8 = new JPanel();
		panel_8.setBorder(new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Dow\u00F3d osobisty", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(190, 30, 30)));
		panel_2.add(panel_8);
		panel_8.setLayout(new BorderLayout(0, 0));
		
		lblDocumentid = new JLabel("documentID");
		panel_8.add(lblDocumentid, BorderLayout.NORTH);
		
		JPanel panel_11 = new JPanel();
		panel_10.add(panel_11, BorderLayout.SOUTH);
		
		JLabel label = new JLabel("Wynajem na okres: ");
		
		spinner = new JSpinner();
		
		dateChooser = new JDateChooser();
		//dateSpinner = new JSpinnerDateEditor();
		//dateChooser.add(dateSpinner);
		
		JLabel label_1 = new JLabel("Cena Całkowita:");
		
		JLabel lblTerminZwrotu = new JLabel("Termin Zwrotu:");
		
		lblzl = new JLabel("11239zl");
		lblzl.setFont(new Font("Arial Black", Font.BOLD, 14));
		GroupLayout gl_panel_11 = new GroupLayout(panel_11);
		gl_panel_11.setHorizontalGroup(
			gl_panel_11.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_11.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_11.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(label_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(label, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
						.addComponent(lblTerminZwrotu, Alignment.LEADING))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_11.createParallelGroup(Alignment.LEADING)
						.addComponent(dateChooser, GroupLayout.PREFERRED_SIZE, 227, GroupLayout.PREFERRED_SIZE)
						.addComponent(spinner, GroupLayout.PREFERRED_SIZE, 169, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblzl))
					.addContainerGap(125, Short.MAX_VALUE))
		);
		gl_panel_11.setVerticalGroup(
			gl_panel_11.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_11.createSequentialGroup()
					.addGroup(gl_panel_11.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_11.createSequentialGroup()
							.addComponent(spinner, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(dateChooser, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel_11.createSequentialGroup()
							.addComponent(label, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(lblTerminZwrotu)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_11.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblzl))
					.addGap(27))
		);
		panel_11.setLayout(gl_panel_11);
		
		JPanel panel_12 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_12.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		panel.add(panel_12, BorderLayout.SOUTH);
		
		btnAnuluj = new JButton("Zamknij");
		btnAnuluj.addActionListener(this);
		panel_12.add(btnAnuluj);
		
		btnDalej = new JButton("Zapisz");
		btnDalej.addActionListener(this);
		panel_12.add(btnDalej);
		
		btnZapiszZamknij = new JButton("Zapisz i zamknij");
		btnZapiszZamknij.addActionListener(this);
		panel_12.add(btnZapiszZamknij);
	}

	private Transaction object;
	private Client client;
	private Car car;
	
	private boolean objectSet=false;
	private JLabel lblName;
	private JLabel lblSurname;
	private JLabel lblDocumentid;
	private JSpinner spinner;
	private JDateChooser dateChooser;
	private JLabel lblzl;
	private JButton btnAnuluj;
	private JButton btnDalej;
	private JLabel lblModel;
	private JLabel lblMaker;
	private JLabel lblDeposit;
	private JLabel lblPrice;
	private JButton btnZapiszZamknij;
	
	
	
	public TransactionCreator(Client client,Car car){
		this();
		
		this.client = client;
		this.car = car;
		
		this.setMinimumSize(new Dimension(700,450));
		this.addWindowListener	(new WindowAdapter(){ 
			public void windowClosing(WindowEvent e){ 
				setVisible(false);				
			}	
		});	

		Dimension dialogSize = getSize();		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
		if(dialogSize.height > screenSize.height) 
			dialogSize.height = screenSize.height;
		if(dialogSize.width > screenSize.width)
			dialogSize.height = screenSize.width;
		setLocation((screenSize.width-dialogSize.width)/2,   
						(screenSize.height-dialogSize.height)/2);
		spinner.addChangeListener(this);
		
		dateChooser.setDateFormatString("yyyy-MM-dd");
		dateChooser.addPropertyChangeListener("date", this);
		
	}
	
	private void emptyFiled(){
		lblzl.setText("0 zł");
		spinner.setValue(0);
		dateChooser.setDate(null);
	}
	
	private void setInitialData(){
		
		lblName.setText(client.getFirstName());
		lblSurname.setText(client.getLastName());
		lblDocumentid.setText(client.getDocumentID());
		
		lblModel.setText(car.getModel());
		lblMaker.setText(car.getMake());
		lblDeposit.setText(Integer.toString((car.getDeposit())));
		lblPrice.setText(car.getPrice().toString());
		
		if(isObjectSet()){
			dateChooser.setDate(object.getDeadlineDate());
		}
	}
	
	private void fillObjectWithData(){
		if(!isObjectSet()){
			object = new Transaction();
		}
		object.setCar(car);
		object.setClient(client);
		object.setPickDate(new Date());
		object.setDeadlineDate(dateChooser.getDate());
		object.setPrice(car.getPrice().multiply(new BigDecimal((Integer)spinner.getValue())));
		object.setPickWorker((Worker) SessionContext.getInstance().getWorker());
	}
	
	@Override
	public void done() {
			fillObjectWithData();
			Session session = HibernateUtil.getSessionFactory().openSession();
			org.hibernate.Transaction transaction = session.beginTransaction();
			session.saveOrUpdate(object);
			transaction.commit();
			session.close();
			object = null;
			objectSet = false;

	}

	@Override
	public void setObject(Transaction object) {
		this.object = object;
		this.car = object.getCar();
		this.client = object.getClient();
		objectSet = true;
		
	}

	@Override
	public boolean isObjectSet() {
		// TODO Auto-generated method stub
		return objectSet;
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		
		
			Integer value = (Integer)spinner.getValue();
			int days = value.intValue();
			
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DAY_OF_YEAR, days);
			dateChooser.setDate(cal.getTime());
			
			
			lblzl.setText(car.getPrice().multiply(new BigDecimal(days)).toString() + " zł");
			
			
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		Calendar cal = Calendar.getInstance();
		int currentday = cal.get(Calendar.DAY_OF_YEAR);
		try{
		cal.setTime(dateChooser.getDate());
		int days = cal.get(Calendar.DAY_OF_YEAR) - currentday;
		spinner.setValue(days);
		}catch(NullPointerException nil){
			
		}
		
	}

	@Override
	public void showCreator() {
		emptyFiled();
		setInitialData();
		dateChooser.setDate(new Date());
		this.setVisible(true);
		
	}
	public void setClientCar(Client client, Car car){
		this.client = client;
		this.car = car;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if(event.getSource() == btnAnuluj){
			object = null;
			objectSet = false;
			dispose();
		}else if(event.getSource()==btnDalej){
			done();
		}else if(event.getSource()==btnZapiszZamknij){
			done();
			object = null;
			objectSet = false;
			dispose();
			
		}
		
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		object = null;
		objectSet = false;
		dispose();
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
